package SellCar.controller;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.HttpRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import SellCar.service.UserBiz;
import SellCar.service.impl.UserBizImpl;

@Controller
/*@RequestMapping("/login")*/
public class UserController {
	@Autowired
	@Qualifier("userBiz")
	private UserBizImpl userbiz;
	ModelAndView mav = new ModelAndView();
	//登录效验
	@RequestMapping(value="/check.html")
	public ModelAndView check(@RequestParam("uname")String userName,
			@RequestParam("upwd")String password,HttpServletRequest request){
		if(userbiz.panduan(userName, password)){
			//mav.addObject("userName", userName);
			//mav.setViewName("guide");
			HttpSession session = request.getSession();
			session.setAttribute("userName", userName);
			return new ModelAndView("redirect:news");
		}else{
			mav.addObject("shuoming", "用户名与密码不匹配请重新登录");
			mav.setViewName("Error");
			return mav;
		}
	}
	
	//注册用户
	@RequestMapping(value="/addZhuCe.html")
	public ModelAndView addZhuCe(@RequestParam("name")String userName,
								 @RequestParam("shenfen")Integer idCardno,
								 @RequestParam("pw1")String pw1,
								 @RequestParam("pw2")String pw2){
		if(pw1.equals(pw2)){
			if(userbiz.panduan1(userName, pw1, idCardno)){
				mav.addObject("shuoming", "注册成功，可以登录了！");
				mav.setViewName("login");
			}else{
				mav.addObject("shuoming", "该用户已存在，请重新注册！");
				mav.setViewName("zhuce");
			}
		}else{
			mav.addObject("shuoming", "确认密码错误，请重新输入");
			mav.setViewName("zhuce");
		}
		return mav;
	}
	
}
